package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CustomerTraceHistory;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.CustomerTraceHistoryQueryObject;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ICustomerTraceHistoryService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customerTraceHistory")
public class CustomerTraceHistoryController {
    @Autowired
    private ICustomerTraceHistoryService customerTraceHistoryService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/list")
    //@RequiresPermissions(value={"潜在客户列表","customerTraceHistory:list"},logical= Logical.OR)
    public String list(@ModelAttribute("qo") CustomerTraceHistoryQueryObject qo, Model model){
        //获取当前用户,当做存进去数据库录入人的数据
        Subject subject = SecurityUtils.getSubject();
        Employee currentEmployee = (Employee) subject.getPrincipal();


        //数据库取用户
        PageInfo<CustomerTraceHistory> pageResult = customerTraceHistoryService.selectByQuery(qo);
        //List<CustomerTraceHistory> list = customerTraceHistoryService.getAll();
        model.addAttribute("result",pageResult);
        //高查里的销售人员列表,  role里面写了静态字段,可以调用
        List<Employee> sellers = employeeService.queryByRoleSn(new String[]{Role.ROLE_SN_MARKET_MANAGER,Role.ROLE_SN_MARKET});
        model.addAttribute("sellers",sellers);

        //拿到所有工作,给添加页面使用--job
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectBySystemDictionarySn("job");
        model.addAttribute("jobs",jobs);
        //拿到所有来源--source
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectBySystemDictionarySn("source");
        model.addAttribute("sources",sources);
        List<SystemDictionaryItem> traceTypes = systemDictionaryItemService.selectBySystemDictionarySn("communicationMethod");
        model.addAttribute("traceTypes",traceTypes);
        return "customerTraceHistory/list";
    }



//    @RequestMapping("/input")
//    @RequiredPermission({"潜在客户添加","customerTraceHistory:input"})
//    public String input(Long id, Model model){
//        if(id != null){
//            CustomerTraceHistory customerTraceHistory = customerTraceHistoryService.getCustomerTraceHistoryById(id);
//            model.addAttribute("entity",customerTraceHistory);
//        }
//        return "customerTraceHistory/input";
//    }
    @RequestMapping("/saveOrUpdate")
    //@RequiresPermissions(value={"潜在客户保存或更新","customerTraceHistory:saveOrUpdate"},logical= Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(CustomerTraceHistory customerTraceHistory){
        try {
            customerTraceHistoryService.saveOrUpdate(customerTraceHistory);
            return new JsonResult();
        }catch(Exception e){
            e.printStackTrace();
            return new JsonResult(false,"保存失败");
        }
    }

}
